package class3;

/**
 * 约瑟夫环问题
 * 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
 * 计算公式：
 * 老 = (新 +s -1) % n +1
 * 老：老编号
 * 新：新编号
 * s：被杀节点编号
 * n: 总结点长度
 */
public class Code15_josehusKill {
    public static void main(String[] args) {
        int compute = compute2(10, 3);
        System.out.println("compute = " + compute);

        int i = compute(1, 10, 3);
        System.out.println("i = " + i);
    }

    public static int compute2(int n, int k) {
        int ans = 1;
        for (int c = 2; c <= n; c++) {
            ans = (ans + k - 1) % c + 1;
            System.out.println(ans);
        }
        return ans;
    }

    /**
     *
     */
    public static int compute(int ans, int n, int k) {
        return (ans + k - 1) % n + 1;
    }
}
